<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="isHide ? 1 : 6" :xs="24" class="relative">
        <div class="app-container-tree-border">
          <project-tree-select-inner class="cd-mb8" :projectId="projectId" @changeProject="changeProject" />
          <el-input class="cd-mb8" v-model="filterText" placeholder="请输入" clearable size="small"
            prefix-icon="el-icon-search" />
          <el-tree class="input-tree" :data="treeList" :props="defaultProps" :filter-node-method="filterNode" ref="tree"
            node-key="id" highlight-current :default-expanded-keys="defaultExpandedKeys" @node-click="handleNodeClick">
            <div slot-scope="{ node, data }" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
          <div class="tree-hide-button iconfont" :class="isHide ? 'icon-xiangyou' : 'icon-xiangzuo'"
            @click="() => { isHide = !isHide }">
          </div>
        </div>
      </el-col>
      <el-col :span="isHide ? 23 : 18" :xs="24">
        <div class="app-container-table-border">
          <el-table border stripe v-loading="loading" :data="dataList">
            <el-table-column type="index" label="序号" width="55" align="center" />
            <el-table-column label="期号名称" align="center" prop="qhName" min-width="120" />
            <el-table-column label="开始时间" align="center" prop="beginDate" min-width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.beginDate, "{y}-{m}-{d}") }}</span>
              </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="endDate" min-width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
              </template>
            </el-table-column>
            <el-table-column label="是否当前期" align="center" prop="currentQhFlag" min-width="100">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_number_yes_no" :value="scope.row.currentQhFlag" />
              </template>
            </el-table-column>
            <el-table-column label="审批状态" align="center" prop="workflowsStateus" min-width="100">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.report_status" :value="scope.row.workflowsStateus" />
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-view" @click="toView(scope.row)">详情</el-button>
                <el-button v-if="canOperation(scope.row)" size="mini" type="text" icon="el-icon-document"
                  @click="onExamine(scope.row)" v-hasPermi="['ma:paymentReportForm:approve']">发起审批</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
    </el-row>
    <examine-and-approve ref="examineAndApproveRef" @callBack="handleQuery" />
  </div>
</template>

<script>
import { querySupplierTreeList } from "@/api/base/bsBd";
import { mapGetters } from "vuex";
import { listReportFormPayment } from "@/api/ma/paymentReportForm";

import ExamineAndApprove from "./examine-and-approve.vue";
export default {
  components: { ExamineAndApprove },
  name: "PaymentReportForm",
  dicts: ["sys_number_yes_no", "report_status"],
  data () {
    return {
      isHide: false,
      treeList: [],
      treeInfo: {},
      defaultExpandedKeys: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      filterText: undefined,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料计量期号表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bdId: undefined,
        gysId: undefined
      }
    };
  },
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      // 系统顶部选中项目变化
      this.changeProject(newVal);
    },
    filterText (val) {
      this.$refs.tree.filter(val);
    }
  },
  created () {
    this.changeProject();
  },
  methods: {
    changeProject (projectId = this.sysProjectId) {
      this.projectId = projectId;
      this.getBdTree();
    },
    getBdTree () {
      querySupplierTreeList({ projectId: this.projectId }).then((response) => {
        this.treeList = response.data;
        // 选中左侧树
        this.$nextTick(() => {
          if (this.treeList.length > 0) {
            // 支付报表详情返回至列表页面时，若页面未加载则默认选中左侧树
            if (this.$route.query.gysId && this.$route.query.zdId) {
              this.defaultExpandedKeys = [this.$route.query.zdId];
              this.$refs.tree.setCurrentKey(this.$route.query.gysId);
              this.queryParams.zdId = this.$route.query.zdId; // 站点ID
              this.queryParams.gysId = this.$route.query.gysId; // 供应商ID
              this.handleQuery();
            }
            // this.$refs.tree.setCurrentKey(this.treeList[0].id);
            // this.handleNodeClick(this.treeList[0]);
          } else {
            this.treeInfo = {};
            this.queryParams.bdId = null;
            this.queryParams.zdId = null;
            this.dataList = [];
            this.total = 0;
          }
        });
      });
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick (data) {
      this.treeInfo = data;
      if (data.type == "supplier") {
        this.queryParams.zdId = data.parentId; // 站点ID
        this.queryParams.gysId = data.id; // 供应商ID
        this.handleQuery();
      } else {
        // this.$message.warning("请选择供应商！");
      }
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询材料计量期号列表 */
    getList () {
      this.loading = true;
      listReportFormPayment(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否可操作
    canOperation (row) {
      return row.workflowsStateus == "00" || row.workflowsStateus == "33";
    },
    // 详情
    toView (row) {
      // const { href } = this.$router.resolve({
      //   name: "fileView",
      //   query: {
      //     reportId: row.reportId,
      //     gysId: row.gysId,
      //   }
      // });
      // window.open(href, "_blank");
      this.$router.push({
        path: "/material/fileView",
        query: {
          reportId: row.reportId
        }
      });
    },
    /** 发起审批 */
    onExamine (row) {
      const projectInfo = this.sysProjectList.find((item) => item.projectId == this.sysProjectId);
      const info = {
        projectId: this.sysProjectId,
        projectName: projectInfo.projectName,
        bdId: row.bdId,
        zdId: row.zdId,
        gysId: row.gysId,
        reportId: row.reportId,
        measureQhId: row.measureQhId
      };
      this.$refs.examineAndApproveRef.init(info);
    }
  }
};
</script>
